package com.oa.ylms.apps.business.regulations.impl;

import java.util.List;

import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.regulations.IRegulationCategoryService;
import com.oa.ylms.apps.business.regulations.IRegulationInfoService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.dao.regulations.IRegulationInfoDao;
import com.oa.ylms.apps.entity.archives.ArchiveInfoValue;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.regulations.RegulationInfoValue;

public class RegulationInfoServiceImpl implements IRegulationInfoService{

	private IRegulationInfoDao infoDao;
    private IRegulationCategoryService categoryService;
	private RegulationInfoValue regulationInfo;
	private IStaffInfoService staffInfoService;
	private ArchiveInfoValue archiveInfo;

	
	
	public int addRegulationInfo(int categoryId, String title,String content,StaffInfoValue staffInfo) throws Exception {
		regulationInfo.setRegulationCategory(categoryService.queryCategoryById(categoryId));
		regulationInfo.setStaffInfo(staffInfo);
		regulationInfo.setTitle(title);
		regulationInfo.setContent(content);
		regulationInfo.setStaffInfo(staffInfo);
		regulationInfo.setPublishTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		return infoDao.addRegulationInfo(regulationInfo);
	}

	public int countTPages(Integer categoryId,String publishTime, String title, String staffName, int rows) throws Exception {
		if(categoryId != null){
			regulationInfo.setRegulationCategory(categoryService.queryCategoryById(categoryId));
		}
		if(publishTime != null){
			regulationInfo.setPublishTime(DateUtil.toDateWithoutHours(publishTime));
		}
		if(title != null){
			title = "%"+title+"%";
			regulationInfo.setTitle(title);
		}
		if(staffName != null){
			staffName = "%"+staffName+"%";
			archiveInfo.setStaffName(staffName);
		}
		return infoDao.countTPages(regulationInfo,archiveInfo, rows);
	}

	public boolean deleteRegulationInfo(String regulationIds) throws Exception {
		String[] regulationIdStr = regulationIds.split(",");
		for(String regulationId : regulationIdStr){
			regulationInfo.setRegulationId(Integer.parseInt(regulationId));
			if(regulationInfo!=null){
				infoDao.deleteRegulationInfo(regulationInfo);
			}else{
				return false;
			}
		}
		return true;
	}

	
	@Override
	public boolean modifyRegulationInfo(int regulationId, int categoryId, String title,String content,StaffInfoValue staffInfo) throws Exception {
		regulationInfo = queryRegulationById(regulationId);
		if(regulationInfo != null){
			regulationInfo.setRegulationCategory(categoryService.queryCategoryById(categoryId));
			regulationInfo.setTitle(title);
			regulationInfo.setContent(content);
			regulationInfo.setStaffInfo(staffInfo);
			return infoDao.modifyRegulationInfo(regulationInfo);
		}
		return false;
	} 

	public List<RegulationInfoValue> queryRegulationsWithPage(Integer categoryId,String publishTime,String title, String staffName, int rows, int pageNo) throws Exception {
		if(categoryId != null){
			regulationInfo.setRegulationCategory(categoryService.queryCategoryById(categoryId));
			}
			if(publishTime != null){
				regulationInfo.setPublishTime(DateUtil.toDateWithoutHours(publishTime));
			}
			if(title != null){
				title = "%"+title+"%";
				regulationInfo.setTitle(title);
			}
			if(staffName != null){
				staffName = "%"+staffName+"%";
				archiveInfo.setStaffName(staffName);
			}
			return infoDao.qureyRegulationsWithPage(regulationInfo, archiveInfo, rows, pageNo);
	}

	public RegulationInfoValue queryRegulationById(int regulationId)throws Exception {
		regulationInfo.setRegulationId(regulationId);
		return infoDao.qureyRegulationById(regulationInfo);
	}

	public List<RegulationInfoValue> queryTopRegulation(int topNum)
			throws Exception {
		return infoDao.qureyTopRegulation(topNum);
	}
	
	
	@Override
	public int getTotalNumber () throws Exception{
		return infoDao.getTotalNumbers();
	}

	
	//
	public IRegulationInfoDao getInfoDao() {
		return infoDao;
	}

	public void setInfoDao(IRegulationInfoDao infoDao) {
		this.infoDao = infoDao;
	}

	public IRegulationCategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(IRegulationCategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public RegulationInfoValue getRegulationInfo() {
		return regulationInfo;
	}

	public void setRegulationInfo(RegulationInfoValue regulationInfo) {
		this.regulationInfo = regulationInfo;
	}

	public IStaffInfoService getStaffInfoService() {
		return staffInfoService;
	}

	public void setStaffInfoService(IStaffInfoService staffInfoService) {
		this.staffInfoService = staffInfoService;
	}

	public void setArchiveInfo(ArchiveInfoValue archiveInfo) {
		this.archiveInfo = archiveInfo;
	}

	public ArchiveInfoValue getArchiveInfo() {
		return archiveInfo;
	}

}
